//
//  HLDataBaseManager.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/16.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLDataBaseManager.h"
#import "FMDB.h"
#import "HLCollect.h"
#import "HLCourseDetail.h"
#import "HLCourse.h"

#define kHLFMDatabaseManagerPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"HLFMDatabaseManagerPath.sqlite"]

@implementation HLDataBaseManager

static FMDatabase  *_db;

+ (void)initialize
{
    _db = [FMDatabase databaseWithPath:kHLFMDatabaseManagerPath];
    
    if (![_db open]) {
        return;
    }
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_history_record(id integer PRIMARY KEY ,collect blob NOT NULL ,course_id text NOT NULL);"];
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_downloading_file(id integer PRIMARY KEY ,detail blob NOT NULL ,course_id text NOT NULL);"];
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_downloaded_file(id integer PRIMARY KEY ,detail blob NOT NULL ,course_id text NOT NULL);"];
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_home_data(id integer PRIMARY KEY ,course blob NOT NULL ,type text NOT NULL);"];
}

/**
 删除

 @param collect 历史记录
 */
+ (void)removeHistoryRecord:(HLCollect *)collect
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_history_record WHERE course_id = %@;",collect.course_id];
    
    if (result) {
        NSLog(@"成功删除 %@",collect.course_name);
    }
}

+ (void)removeDownloadingFile:(HLCourseDetail *)courseDetail
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_downloading_file WHERE course_id = %@;",courseDetail.ID];
    
    if (result) {
        NSLog(@"成功删除  正在下载的 courseDetail %@",courseDetail.name);
    }
}

+ (void)removeDownloadedFile:(HLCourseDetail *)courseDetail
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_downloaded_file WHERE course_id = %@;",courseDetail.ID];
    if (result) {
        NSLog(@"成功删除 已存在的 courseDetail %@",courseDetail.name);
    }
}

+ (void)removeALLDownloadedFile
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_downloaded_file;"];
    if (result) {
        NSLog(@"成功 ------t_downloaded_file 删除  ");
    }
}

+ (void)removeALLDownloadingFile
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_downloading_file;"];
    if (result) {
        NSLog(@"成功 ------t_downloading_file 删除 ");
    }
}

+ (void)removeALLHistoryRecord
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_history_record;"];
    if (result) {
        NSLog(@"成功 ------removeALLHistoryRecord 删除 所有历史记录 ");
    }
}

+ (void)removeALLHomeDatas
{
    BOOL result = [_db executeUpdateWithFormat:@"DELETE FROM t_home_data;"];
    if (result) {
        NSLog(@"成功 ------t_home_data 删除 主页缓存数据 ");
    }
}


+ (void)updateDownloadingFile:(HLCourseDetail *)courseDetail
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:courseDetail];
    
    NSString *sqlStr = [NSString stringWithFormat:@"UPDATE t_downloading_file SET detail = ? WHERE course_id = ?;"];
    BOOL result = [_db executeUpdate:sqlStr,data,courseDetail.ID];
    if (result) {
        NSLog(@"更新成功 courseDetail %@",courseDetail.name);
    }
}


/**
 添加

 @param collect 视频播放模型
 */
+ (void)addHistoryRecord:(HLCollect *)collect
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:collect];
    BOOL result =  [_db executeUpdateWithFormat:@"INSERT INTO t_history_record (collect,course_id) VALUES(%@,%@);",data,collect.course_id];
    if (result) {
        NSLog(@"成功插入 collect %@",collect.course_name);
    }
}

/**
 * 主页数据缓存
 @param course course
 */

+ (void)addHomeDataCourse:(HLCourse *)course type:(NSInteger)type
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:course];
    BOOL result =  [_db executeUpdateWithFormat:@"INSERT INTO t_home_data (course,type) VALUES(%@,%@);",data,[NSString stringWithFormat:@"%zd",type]];
    if (result) {
        NSLog(@"成功插入 coll数据缓存ect --111-%@",@" 主页数据");
    }
}


+ (void)addDownloadingFile:(HLCourseDetail *)courseDetail
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:courseDetail];
    
    BOOL result =  [_db executeUpdateWithFormat:@"INSERT INTO t_downloading_file (detail,course_id) VALUES(%@,%@);",data,courseDetail.ID];
    if (result) {
        NSLog(@"成功插入 正在下载 %@",courseDetail.name);
    }
}

+ (void)addDownloadedFile:(HLCourseDetail *)courseDetail
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:courseDetail];
    BOOL result =  [_db executeUpdateWithFormat:@"INSERT INTO t_downloaded_file (detail,course_id) VALUES(%@,%@);",data,courseDetail.ID];
    if (result) {
        NSLog(@"成功插入 已经 下载 %@",courseDetail.name);
    }
}

/**
 查询

 * 页码
 @return 模型数组
 */
+ (NSArray*)homeDataCourse:(NSInteger)type
{
//    select *from 数据表名 where 值='' and 值='';
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT *FROM t_home_data WHERE type = %@",[NSString stringWithFormat:@"%zd",type]];
    NSMutableArray *courses = [NSMutableArray array];
    while (set.next) {
        HLCourse *collect = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"course"]];
        [courses addObject:collect];
    }
    return courses;
}




+ (NSArray*)historyRecord:(NSInteger)page
{
    int size = 100000;
    int pos = ((int)page - 1)*size;
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT *FROM t_history_record ORDER BY id DESC LIMIT %d,%d",pos,size];
    NSMutableArray *collects = [NSMutableArray array];
    while (set.next) {
        HLCollect *collect = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"collect"]];
        [collects addObject:collect];
    }
    return collects;
}

+ (NSArray*)downloadingFiles:(NSInteger)page
{
    int size = 100000;
    int pos = ((int)page - 1)*size;
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT *FROM t_downloading_file ORDER BY id DESC LIMIT %d,%d",pos,size];
    NSMutableArray *details = [NSMutableArray array];
    while (set.next) {
        HLCourseDetail *detail = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"detail"]];
        [details addObject:detail];
    }
    return details;
}

+ (NSArray*)downloadedFiles:(NSInteger)page
{
    int size = 100000;
    int pos = ((int)page - 1)*size;
    FMResultSet *set = [_db executeQueryWithFormat:@"SELECT *FROM t_downloaded_file ORDER BY id DESC LIMIT %d,%d",pos,size];
    NSMutableArray *details = [NSMutableArray array];
    while (set.next) {
        HLCourseDetail *detail = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"detail"]];
        [details addObject:detail];
    }
    return details;
}


/**
 判断数据库中是否存在

 @param collect 历史记录模型
 @return 返回是否存在
 */
+ (BOOL)isHistoryRecord:(HLCollect *)collect
{
   FMResultSet *set =  [_db executeQueryWithFormat:@"SELECT count(*) AS collect_count FROM t_history_record WHERE course_id = %@",collect.course_id];
    [set next];
    return [set intForColumn:@"collect_count"] == 1;
}


+ (BOOL)isDownloadingFile:(HLCourseDetail *)courseDetail
{
    FMResultSet *set =  [_db executeQueryWithFormat:@"SELECT count(*) AS detail_count FROM t_downloading_file WHERE course_id = %@",courseDetail.ID];
    [set next];
    return [set intForColumn:@"detail_count"] == 1;
}


+ (BOOL)isDownloadedFile:(HLCourseDetail *)courseDetail
{
    FMResultSet *set =  [_db executeQueryWithFormat:@"SELECT count(*) AS detail_count FROM t_downloaded_file WHERE course_id = %@",courseDetail.ID];
    [set next];
    return [set intForColumn:@"detail_count"] == 1;
}


@end
